package com.pkx.guide.system.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pkx.guide.common.annotation.ORpose;
import com.pkx.guide.common.annotation.PreventDuplicateSubmit;
import com.pkx.guide.common.model.Option;
import com.pkx.guide.system.model.form.DictForm;
import com.pkx.guide.system.model.form.DictTypeForm;
import com.pkx.guide.system.model.query.DictPageQuery;
import com.pkx.guide.system.model.query.DictTypePageQuery;
import com.pkx.guide.system.model.vo.DictPageVO;
import com.pkx.guide.system.model.vo.DictTypePageVO;
import com.pkx.guide.system.service.TSysDictService;
import com.pkx.guide.system.service.TSysDictTypeService;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import jakarta.annotation.Resource;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name = "字典接口")
@RestController
@RequestMapping("/dict")
@ORpose
public class TSysDictController {

    @Resource
    private TSysDictService dictService;

    @Resource
    private TSysDictTypeService dictTypeService;

    @Operation(summary = "字典分页列表")
    @GetMapping("/page")
    public Page<DictPageVO> getDictPage(@ParameterObject DictPageQuery queryParams) {
        return dictService.getDictPage(queryParams);
    }

    @Operation(summary = "字典数据表单数据")
    @GetMapping("/{id}/form")
    public DictForm getDictForm(@Parameter(description ="字典ID") @PathVariable Long id) {
        return dictService.getDictForm(id);
    }

    @Operation(summary = "新增字典")
    @PostMapping
    @PreAuthorize("@ss.hasPerm('sys:dict:add')")
    @PreventDuplicateSubmit
    public Boolean saveDict(@RequestBody DictForm DictForm) {
        return dictService.saveDict(DictForm);
    }

    @Operation(summary = "修改字典")
    @PutMapping("/{id}")
    @PreAuthorize("@ss.hasPerm('sys:dict:edit')")
    public Boolean updateDict(@PathVariable Long id, @RequestBody DictForm DictForm) {
        return dictService.updateDict(id, DictForm);
    }

    @Operation(summary = "删除字典")
    @DeleteMapping("/{ids}")
    @PreAuthorize("@ss.hasPerm('sys:dict:delete')")
    public Boolean deleteDict(@Parameter(description ="字典ID，多个以英文逗号(,)拼接") @PathVariable String ids) {
        return dictService.deleteDict(ids);
    }


    @Operation(summary = "字典下拉列表")
    @GetMapping("/{typeCode}/options")
    public List<Option> listDictOptions(@Parameter(description ="字典类型编码") @PathVariable String typeCode) {
        return dictService.listDictOptions(typeCode);
    }

    @Operation(summary = "字典类型分页列表")
    @GetMapping("/types/page")
    public Page<DictTypePageVO> getDictTypePage(@ParameterObject DictTypePageQuery queryParams) {
        return dictTypeService.getDictTypePage(queryParams);
    }

    @Operation(summary = "字典类型表单数据")
    @GetMapping("/types/{id}/form")
    public DictTypeForm getDictTypeForm(@Parameter(description ="字典ID") @PathVariable Long id) {
        return dictTypeService.getDictTypeForm(id);
    }

    @Operation(summary = "新增字典类型")
    @PostMapping("/types")
    @PreAuthorize("@ss.hasPerm('sys:dict_type:add')")
    @PreventDuplicateSubmit
    public Boolean saveDictType(@RequestBody DictTypeForm dictTypeForm) {
        return dictTypeService.saveDictType(dictTypeForm);
    }

    @Operation(summary = "修改字典类型")
    @PutMapping("/types/{id}")
    @PreAuthorize("@ss.hasPerm('sys:dict_type:edit')")
    public Boolean updateDictType(@PathVariable Long id, @RequestBody DictTypeForm dictTypeForm) {
        return dictTypeService.updateDictType(id, dictTypeForm);
    }

    @Operation(summary = "删除字典类型")
    @DeleteMapping("/types/{ids}")
    @PreAuthorize("@ss.hasPerm('sys:dict_type:delete')")
    public Boolean deleteDictTypes(@Parameter(description ="字典类型ID，多个以英文逗号(,)分割") @PathVariable String ids) {
        return dictTypeService.deleteDictTypes(ids);
    }

}
